import 'package:banyou_app/app/request/request_main.dart';
import 'package:banyou_app/app/routes/app_pages.dart';
import 'package:banyou_app/app/util/common_utils.dart';
import 'package:banyou_app/app/util/string_util.dart';
import 'package:banyou_app/app/widget/delete_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class MineController extends GetxController {
  final mobile = ''.obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    // if (box.read('mydongtai') == null) {
    box.write('mydongtai', [
      {
        "Content": "天空下雨了，可以打伞；心下雨了，该怎么办呢？",
        "AddTime": "5天前",
        'Distance': '4.9KM',
        'UserGender': "男",
        "PictureUrl":
            "https://aqcdn.olbibo.com.cn/Qa/19246444d94f081e3549803b928260f56.png",
        "ViewNum": 278,
        "Comments": [
          {
            "Id": 10011,
            "UserAvatar":
                "https://aqcdn.olbibo.com.cn/Qa/0a2369958a9645eac52b58a8134e2ef5a.png",
            "UserName": "老黑",
            "Content": "风景如画,令人心旷神怡",
            "AddTime": "9天前"
          },
          {
            "Id": 10012,
            "UserAvatar":
                "https://aqcdn.olbibo.com.cn/Qa/0d3ab9b41f98222ad7b5ff8a822172796.png",
            "UserName": "黑夜传说",
            "Content": "让人流连忘返",
            "AddTime": "6小时前"
          },
          {
            "Id": 10013,
            "UserAvatar":
                "https://aqcdn.olbibo.com.cn/Qa/00023a1e3447fdb31836536cc903f1310.png",
            "UserName": "酸奶",
            "Content": "这个地方我也想去",
            "AddTime": "2天前"
          }
        ]
      }
    ]);
    // }

    super.onReady();
  }

  loadData() {
    mobile.value = phoneNumberStr(box.read('mobile'));
  }

  // 退出
  goExit() {
    Get.dialog(DeleteDialogView('确定要退出登录吗？', 'assets/images/组 84@2x.png'),
            barrierColor: Colors.black.withAlpha(100))
        .then((value) async {
      if (value == 1) {
        EasyLoading.show(status: '退出中...');
        Map? m = await RequestMain.postUrl({'method': box.read('mobile')},
            turl: 'https://aqbanyou.olbibo.com.cn/sign/?a=index');
        if (m.isNotEmpty && m['code'].toString() == "200") {
          await box.remove('key');
          await box.remove('kefu');
          await box.remove('tab');
          await box.remove('oneKey');
          await box.remove('mobile');

          EasyLoading.showToast('退出成功');
          Get.offAndToNamed(Routes.LOGIN);
        } else {
          EasyLoading.dismiss();
        }
      }
    });
  }

  toInfo() {}
  //
  clickItem(int index) {
    if (index == 0) {
      dynamic key = box.read("key");
      if (null == key) {
        Get.offAndToNamed(Routes.LOGIN);
        return;
      }
      Get.toNamed(Routes.MYDONGTAI, arguments: {'type': 1});
    } else if (index == 1) {
      dynamic key = box.read("key");
      if (null == key) {
        Get.offAndToNamed(Routes.LOGIN);
        return;
      }
      Get.toNamed(Routes.KEFU);
    } else if (index == 2) {
      dynamic key = box.read("key");
      if (null == key) {
        Get.offAndToNamed(Routes.LOGIN);
        return;
      }
      goExit();
    } else if (index == 3) {
      dynamic key = box.read("key");
      if (null == key) {
        Get.offAndToNamed(Routes.LOGIN);
        return;
      }
      Get.toNamed(Routes.ZHUXIAO);
    } else if (index == 4) {
      Get.toNamed(Routes.WEB, arguments: {
        "title": '隐私政策',
        "url": 'https://aqcdn.olbibo.com.cn/Static/PrivacyPolicy.html',
      });
    }
  }

  @override
  void onClose() {}
}
